<?php

declare(strict_types=1);

namespace app\validate\api\admin\system;

use app\validate\api\ApiBaseValidate;
use app\model\api\admin\system\AdminAuthRule as MenuModel;

class MenuValidate extends ApiBaseValidate
{
    protected $ex_data = [
    ];
    protected $rule = [
        'id' => 'require|integer',
        'name' => 'requireCallback:checkNameRequire|max:80',
        'title' => 'require|max:20',
        'type' => 'in:1,2',
        'status' => 'in:0,1',
        // 'create_time' => 'number',
        // 'update_time' => 'number',
        // 'delete_time' => 'number',

        'parent_id' => 'require|integer|egt:0|isParentIdValid',
        'menu_type' => 'in:0,1,2,3',
        // 'title2' => 'max:100',
        // 'name2' => 'max:100',
        'path' => 'max:255',
        'component' => 'max:255',
        'rank' => 'integer',
        'redirect' => 'max:255',
        'icon' => 'max:32',
        'extra_icon' => 'max:32',
        // 'transition2' => 'max:255',
        'enter_transition' => 'max:64',
        'leave_transition' => 'max:64',
        'active_path' => 'max:255',
        'auths' => 'max:512',
        'roles' => 'max:255',
        'frame_src' => 'max:255',
        'frame_loading' => 'boolean',
        'keep_alive' => 'boolean',
        'hidden_tag' => 'boolean',
        'fixed_tag' => 'boolean',
        'show_link' => 'boolean',
        'show_parent' => 'boolean',
        'dynamic_level' => 'integer',

        // 扩展
        'del_ids' => 'require|regex:ids',
    ];

    protected $message = [];

    protected $scene = [
        'save' => ['name', 'title', 'type', 'status', 'parent_id', 'menu_type', 'path', 'component', 'rank', 'redirect', 'icon', 'extra_icon', 'enter_transition', 'leave_transition', 'active_path', 'auths', 'roles', 'frame_src', 'frame_loading', 'keep_alive', 'hidden_tag', 'fixed_tag', 'show_link', 'show_parent', 'dynamic_level'],
        'update' => ['id', 'name', 'title', 'type', 'status', 'parent_id', 'menu_type', 'path', 'component', 'rank', 'redirect', 'icon', 'extra_icon', 'enter_transition', 'leave_transition', 'active_path', 'auths', 'roles', 'frame_src', 'frame_loading', 'keep_alive', 'hidden_tag', 'fixed_tag', 'show_link', 'show_parent', 'dynamic_level'],
    ];

    public function sceneDelete()
    {
        return $this->only(
            [
                'del_ids',
            ]
        )->append('del_ids', 'hasChild');
    }

    protected function hasChild($value, $rule, $data = [])
    {
        $m = MenuModel::where([
            ['parent_id', 'in', $data['del_ids']]
        ])
            ->field('id')
            ->select();

        if (!$m->isEmpty()) {
            return '存在子菜单/权限，无法删除';
        }
        return true;
    }

    function checkNameRequire($value, $data)
    {
        if ($data['menu_type'] != 3) {
            return true;
        }
    }

    protected function isParentIdValid($value, $rule, $data = [], $field = '', $title = '')
    {
        if ($value == 0) {
            return true;
        }

        if (isset($data['id']) && $data['id'] != '') {
            if ($data['id'] == $data['parent_id']) {
                return '不能选择自己作为父级';
            }
        }
        $m = MenuModel::where([
            ['id', '=', $value],
            ['status', '=', MenuModel::STATUS_NOMAL],
        ])
            ->field('id')
            ->find();
        if (!$m) {
            return '父级菜单不存在或已禁用';
        }
        return true;
    }
}
